// import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:month_picker_dialog/month_picker_dialog.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sticky_headers/sticky_headers/widget.dart';
import 'package:yi_shop/model/invoice.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';
import 'package:yi_shop/utils/tools.dart';

class InvoiceAssistantController extends GetxController {
  RxList<InvoiceAssistantModel> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  int year = DateTime.now().year - 1;

  @override
  onInit() {
    initData();
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 5) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    var month = DateTime.now().month + 1;
    for (var i = 0; i < 5; i++) {
      pageList.add(InvoiceAssistantModel(month: '$year年$month月', children: [
        AssistantInfoModel(lookUp: '个人', amount: 1998.85, remark: '小米Note3 全网通 6GB+64GB 黑'),
        AssistantInfoModel(lookUp: '个人', amount: 1998.85, remark: '小米Note3 全网通 6GB+64GB 黑'),
        AssistantInfoModel(lookUp: '个人', amount: 1998.85, remark: '小米Note3 全网通 6GB+64GB 黑'),
        AssistantInfoModel(lookUp: '个人', amount: 1998.85, remark: '小米Note3 全网通 6GB+64GB 黑'),
      ]));
      month--;
    }
    year--;
  }
}

class InvoiceAssistantPage extends GetView<InvoiceAssistantController> {
  const InvoiceAssistantPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Text(
          '发票助手',
          style: StylesUtils.customTextStyle(
              fontWeight: FontWeight.w700, fontSize: StylesUtils.fontSize18),
        ),
        bottom: PreferredSize(
          preferredSize: const Size.fromHeight(140),
          child: SizedBox(
            height: 140,
            child: Column(
              children: [
                Container(
                  height: 100,
                  decoration: BoxDecoration(
                      color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048)),
                  child: Row(
                    children: [
                      Expanded(
                          child: InkWell(
                        onTap: () {
                          Get.toNamed(RoutePath.invoiceRise);
                        },
                        child: Column(
                          children: [
                            Container(
                              margin: const EdgeInsets.only(top: 10),
                              alignment: Alignment.center,
                              child: SvgUtils.assetName('u17398', width: 50, height: 38),
                            ),
                            Container(
                              margin: const EdgeInsets.only(top: 15),
                              alignment: Alignment.center,
                              child: Text(
                                '发票抬头',
                                style: StylesUtils.customTextStyle(),
                              ),
                            )
                          ],
                        ),
                      )),
                      Expanded(
                          child: InkWell(
                        onTap: () {
                          Get.toNamed(RoutePath.invoiceIssue);
                        },
                        child: Column(
                          children: [
                            Container(
                              margin: const EdgeInsets.only(top: 10),
                              alignment: Alignment.center,
                              child: SvgUtils.assetName('u17401', width: 50, height: 38),
                            ),
                            Container(
                              margin: const EdgeInsets.only(top: 15),
                              alignment: Alignment.center,
                              child: Text(
                                '开具发票',
                                style: StylesUtils.customTextStyle(),
                              ),
                            )
                          ],
                        ),
                      ))
                    ],
                  ),
                ),
                Expanded(
                    child: Container(
                  color: Colors.white,
                  padding: EdgeInsets.symmetric(horizontal: 20.w),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      InkWell(
                        onTap: () {
                          Get.toNamed(RoutePath.invoiceExplain);
                        },
                        child: RichText(
                            text: TextSpan(children: [
                          TextSpan(
                              text: '我的发票 ',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color3333)),
                          WidgetSpan(
                              child: SvgUtils.assetName('u17404', width: 18, height: 18),
                              alignment: PlaceholderAlignment.middle)
                        ])),
                      ),
                      IconButton(
                          onPressed: () {
                            Pickers.showDatePicker(context,
                                pickerStyle: PickerStyle(
                                    context: context,
                                    menuHeight: 50,
                                    pickerItemHeight: 50,
                                    title: Center(
                                      child: Text(
                                        '请选择日期',
                                        style: StylesUtils.customTextStyle(
                                            color: ColorsUtils.color3333,
                                            fontSize: StylesUtils.fontSize16),
                                      ),
                                    )
                                ),
                                mode: DateMode.YM,
                                onConfirm: (PDuration res) {});
                          },
                          icon: SvgUtils.assetName('u17405', width: 28, height: 30))
                    ],
                  ),
                ))
              ],
            ),
          ),
        ),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  void openSelectMonth(BuildContext context) {
    showMonthPicker(
            context: context,
            // hideHeaderRow: false,
            // dismissible: true,
            // yearFirst: false,
            // cancelWidget: Text(
            //   '取消',
            //   style: StylesUtils.customTextStyle(
            //     color: ColorsUtils.color9999,
            //   ),
            // ),
            // confirmWidget: Text(
            //   '确定',
            //   style: StylesUtils.textStyle(color: ColorsUtils.rgba(128, 128, 255, 1)),
            // ),
            // initialDate: DateTime.now(),
            // selectedMonthTextColor: Colors.white,
            // selectedMonthBackgroundColor:
            //     ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
            // backgroundColor: Colors.white
    )
        .then((DateTime? date) {});
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      padding: EdgeInsets.symmetric(horizontal: 15.w),
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return StickyHeader(
          header: Container(
            height: 40,
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.symmetric(horizontal: 15.w),
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2),
            child: Text(
              row.month,
              style: StylesUtils.customTextStyle(
                  color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13),
            ),
          ),
          content: Column(
            children: row.children
                .map((e) => Container(
                      margin: EdgeInsets.only(bottom: 10.h),
                      alignment: Alignment.center,
                      child: Container(
                        width: 330.w,
                        padding: EdgeInsets.symmetric(vertical: 10.h, horizontal: 15.w),
                        decoration: BoxDecoration(
                            color: Colors.white, borderRadius: BorderRadius.circular(5.r)),
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Container(
                                  width: 80,
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    '发票抬头',
                                    style: StylesUtils.customTextStyle(
                                        color: ColorsUtils.color9999,
                                        fontSize: StylesUtils.fontSize13),
                                  ),
                                ),
                                Expanded(
                                    child: Container(
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    e.lookUp,
                                    style:
                                        StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                                  ),
                                ))
                              ],
                            ),
                            Container(
                              margin: EdgeInsets.symmetric(vertical: 10.h),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  Container(
                                    width: 80,
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      '开票金额',
                                      style: StylesUtils.customTextStyle(
                                          color: ColorsUtils.color9999,
                                          fontSize: StylesUtils.fontSize13),
                                    ),
                                  ),
                                  Expanded(
                                      child: Container(
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      '￥${e.amount}',
                                      style:
                                          StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                                    ),
                                  ))
                                ],
                              ),
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Container(
                                  width: 80,
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    '开票名称',
                                    style: StylesUtils.customTextStyle(
                                        color: ColorsUtils.color9999,
                                        fontSize: StylesUtils.fontSize13),
                                  ),
                                ),
                                Expanded(
                                    child: Container(
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    e.remark,
                                    style:
                                        StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ))
                              ],
                            ),
                          ],
                        ),
                      ),
                    ))
                .toList(),
          ),
        );
      },
      itemCount: controller.pageList.length,
    );
  }
}
